<!DOCTYPE html>
<html>

<head>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
    <script src="https://code.jquery.com/jquery-2.1.4.min.js"></script>
    <script src="http://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script>
</head>

<body>
    <div class="container">
        <div class="row">
            <div class="col-md-12">
                <h1>JSSDK TEST</h1>

                <h2>
                    <a class="btn btn-default QRScan">扫描</a>
                </h2>
            </div>
        </div>
    </div>
</body>
<script>
    $(document).ready(function () {
        var url = "/config";
        var weixinUrl = location.href.split('#')[0];
        weixinUrl = 'https://jprnrqixad.localtunnel.me/';
        // alert(weixinUrl);
        // alert(JSON.stringify(location));
        $.ajax({
            url: url, //这个地址是服务器配置JSSDK的地址
            data: {
                url: weixinUrl
                //这个地址是发生jssdk调用的url地址
            }
        }).success(function (json) {
            // alert(JSON.stringify(json.data));
            var data = json.data;
            var config = {};
            for (var k in json.data) {
                config[k] = json.data[k];
            }
            // config.debug = true;
            config.jsApiList = [
                'checkJsApi',
                'scanQRCode'
            ];
            config.timestamp = parseInt(config.timestamp);
            wx.config(config);
            wx.ready(function () {
                alert("微信jssdk配置成功!");
                $('h2 > a.QRScan').click(function () {
                    wx.scanQRCode({
                        needResult: 1, // 默认为0，扫描结果由微信处理，1则直接返回扫描结果，
                        scanType: ["qrCode", "barCode"], // 可以指定扫二维码还是一维码，默认二者都有
                        success: function (res) {
                            var result = res.resultStr; // 当needResult 为 1 时，扫码返回的结果
                            alert(result);
                        }
                    });
                });
            });
            wx.error(function (res) {
                alert('微信JSSDK配置失败！');
                alert(JSON.stringify(res));
                alert(res.errMsg);
            });
        });
    });
</script>

</html>